﻿using M.Warehouse.API.Application.Command.BinLocationsCommand;
using M.Warehouse.Domain;
using M.Warehouse.Repository.WarehouseInterface;
using MediatR;

namespace M.Warehouse.API.Application.CommandHandlers.BinLocationsCommandHandlers
{
    public class BinLocationsShowByReservoirIdCommandHandlers : IRequestHandler<BinLocationsShowByReservoirIdCommand, List<BinLocations>>
    {
        /// <summary>
        /// 库位仓储
        /// </summary>
        private readonly IBinLocationsRepository _repository;
        /// <summary>
        /// 库区仓储
        /// </summary>
        private readonly IReservoirsWRepository _reservoirsWRepository;
        public BinLocationsShowByReservoirIdCommandHandlers(IBinLocationsRepository repository, IReservoirsWRepository reservoirsWRepository)
        {
            _repository = repository;
            _reservoirsWRepository = reservoirsWRepository;
        }

        public async Task<List<BinLocations>> Handle(BinLocationsShowByReservoirIdCommand request, CancellationToken cancellationToken)
        {
            var rlist=(await _reservoirsWRepository.GetAllAsync()).Where(x=>!x.IsDeleted&&x.WarehouseId==request.WarehouseId).ToList();
            var blist = (await _repository.GetAllAsync()).Where(x => !x.IsDeleted);
            var list=(
                from reservoir in rlist
                join binlocation in blist
                on reservoir.WarehouseId equals binlocation.WarehouseId
                select binlocation
                ).Distinct().ToList();
            return list;
        }
    }
}
